*** Setup synthetic controls for defendant substitution to OP 
cd /homes/nber/jetson-dua52260/poterba-DUA52260/jetson-dua52260/ipop/substitutes

set seed 10297462

*** For the single hospitals, take a random sample of 100 control OP providers
local dftlist "StJo Wheaton ElCentro Overlook"
foreach dft of local dftlist {

	use op_ipop_controls_panel.dta, clear

	** Random sample 100 providers
	keep if control == 1 
	keep prvdrnum
	duplicates drop 
	sample 100, count
	save "controls/`dft'_control_id.dta", replace 

	use op_ipop_controls_panel.dta, clear
	merge m:1 prvdrnum using "controls/`dft'_control_id.dta", keep(match)
	drop _merge control

	save "controls/`dft'_controls.dta", replace
}

*** For the groups of hospitals, sample with replacement to make 100 groups of that size 
cd /homes/nber/jetson-dua52260/poterba-DUA52260/jetson-dua52260/ipop/substitutes

**MortonPlant, Shands are both 6 hospitals
local dftlist = "MortonPlant Shands"
foreach dft of local dftlist {
	clear all 
	save control_6group_temp.dta, replace emptyok

	
	foreach i of numlist 1/100{
		use op_ipop_controls_panel.dta, clear
		
		keep if control == 1 

		keep prvdrnum
		duplicates drop 
		sample 6, count
		save temp_6.dta, replace

		use op_ipop_controls_panel.dta, clear
		merge m:1 prvdrnum using temp_6.dta, keep(match)
		replace prvdrnum = "`i'"

		collapse (sum) pmt_amt (first) prvdrnum, by(dt)
		append using control_6group_temp.dta

		save control_6group_temp.dta, replace 
	}
	clear all 
	use control_6group_temp.dta, clear
	save "controls/`dft'_controls.dta", replace 
}
rm control_6group_temp.dta 
rm temp_6.dta

cd /homes/nber/jetson-dua52260/poterba-DUA52260/jetson-dua52260/ipop/substitutes
*** CHS is 119 hospitals 
local dftlist = "chs"
foreach dft of local dftlist {
	clear all 
	save control_119group_temp.dta, replace emptyok

	
	foreach i of numlist 1/100{
		use op_ipop_controls_panel.dta, clear
		
		keep if control == 1 

		keep prvdrnum
		duplicates drop 
		sample 119, count
		save temp_119.dta, replace

		use op_ipop_controls_panel.dta, clear
		merge m:1 prvdrnum using temp_119.dta, keep(match)
		replace prvdrnum = "`i'"

		collapse (sum) pmt_amt (first) prvdrnum, by(dt)
		append using control_119group_temp.dta

		save control_119group_temp.dta, replace 
	}
	clear all 
	use control_119group_temp.dta, clear
	save "controls/`dft'_controls.dta", replace 
}
rm control_119group_temp.dta 
rm temp_119.dta
